cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/* control for baseline mobility rates */
/***************/
/* Estimations */
/***************/
use final.dta, clear

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 

xi , pre(REG_) i.nreg

save finalR.dta, replace

capture program drop autoresid
program define autoresid
version 11
syntax, qreg(name)
save tempX.dta, replace
predict resid , residuals
collapse (mean ) resid (count) nobs=resid, by(ze an)
egen indiv=group(ze)
egen time=group(an)
xtset indiv time
display "`qreg'"
gen dresid=D.resid
ivreg2 resid L.resid , cl(ze) noc
estimates store f`qreg'
ivreg2 resid L2.resid , cl(ze) noc
estimates store s`qreg'
use tempX.dta, replace
end

capture program drop regsampBL
program define regsampBL
version 11
syntax, occp(name) outc(name)

use finalR.dta, clear

gen occ="bc" if bc==1
replace occ="cad" if cad==1

/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=(`occp'==0 & L.`occp'==1)
replace oshift=( (cad==0 & L.cad==1) | (tecemp2==0 & L.tecemp2==1) | (bc==0 & L.bc==1) | (bcnq==0 & L.bcnq==1)) if "`occp'"=="all"

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals

/* compute baseline mobility rate */
save tempBL.dta, replace
keep if btime==2
collapse (mean) r`outc' , by(ze an)
rename r`outc' lr`outc'
/* lag the mobility rate */
drop if an==2007
replace an=2007 if an==1999
replace an=1999 if an==1991
replace an=1991 if an==1982
save BLR.dta, replace
use tempBL.dta, clear

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

joinby ze an using BLR.dta, unm(m)

ivreg2 r`outc' (dimm inter = dimm75 inter75) lqinit lr`outc' Y_*  REG_* [aweight = iw2] , partial(Y_* REG_*) cl(ze)
estimates store `occp'ivs
autoresid , qreg(`outc'`occp')
end

capture program drop mobtypBL
program define mobtypBL
version 11
syntax, outc(name) file(name) 

/* OUTFLOWS */

regsampBL, occp(all) outc(`outc')
regsampBL, occp(cad) outc(`outc')
regsampBL, occp(tecemp2) outc(`outc')
regsampBL, occp(bc) outc(`outc')
regsampBL, occp(bcnq) outc(`outc')

/* IVSELECT */
esttab allivs  cadivs tecemp2ivs bcivs bcnqivs /* bcivs tradivs ntradivs constbcivs */ ///
using "C:\Users\Public\Documents\Tab21\TableA8.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
*/
end
capture rm "C:\Users\Public\Documents\Tab21\TableA8.rtf"
mobtypBL , outc(lshift) file(outflows)

/* INFLOWS WITH CONTROLS FOR BASELINE RATES */
capture program drop inflowsBR
program define inflowsBR
version 11
syntax, occp(name)

use unbalanced.dta, clear
replace cad2=(cs2h=="33" | cs2h=="34" | cs2h=="37" | cs2h=="38")

gen orig=(ptreat==1 & `occp'==1 )
/* gen inflow=(dtreat==1 & bc==1 & (L.bc!=1 | L.ize!=ize) & !missing(L.ize)) */
gen inflow=(dtreat==1 & `occp'==1 & L.ize!=ize )

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 

xi , pre(REG_) i.nreg

gen sond=12
replace sond=24 if an==2007

save temp1.dta, replace
collapse (sum) norig=orig ninflow=inflow [fweight = sond], by(ze an)
save p1.dta, replace
use temp1.dta, clear
collapse dimm dimm75 Y_* REG_*, by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen inflr=(D.ninflow)/norig
gen Linflr=(L.ninflow)/L.norig

drop if missing(dimm75)
/* OLS */
ivreg2 inflr Linflr dimm Y_*  REG_*, partial(Y_* REG_*) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 inflr Linflr (dimm  = dimm75 ) Y_* REG_*, partial(Y_* REG_*) cl(ze)
estimates store `occp'iv

end

inflowsBR, occp(all) 
inflowsBR, occp(cad2) 
inflowsBR, occp(tecemp2) 
inflowsBR, occp(bc) 
inflowsBR, occp(bcnq) 

/* IV */
esttab alliv  cad2iv tecemp2iv bciv bcnqiv ///
using "C:\Users\Public\Documents\Tab21\TableA8.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
